--[[
	Copyright (C) Udorn (Blackhand)
--]]

local viewType = "Icon"
local version = 1
local YakaGui = LibStub("YakaGui", true)
local info = YakaGui:GetViewInfo(viewType) 
if (info and info.version >= version) then 
	return 
end

yaka = yaka or {}
yaka.Icon = {}
yaka.Icon.prototype = {}
yaka.Icon.metatable = {__index = yaka.Icon.prototype}
setmetatable(yaka.Icon.prototype, {__index = yaka.View.prototype})

yaka.Icon.info = {viewType = viewType, version = version}
yaka.Icon.GetViewInfo = function()
	return yaka.Icon.info
end

local log = vendor.Debug:new("Icon")

local function _OnClick(this, button)
	local self = this.view
	local info = {arg = self.cfg.arg, view = self, button = button}
	if (self.cfg.onClick) then
		self.cfg.onClick(info)
	end
end

local function _SetTexture(self, name, cfg)
	if (cfg) then
		if (not cfg.file) then
			error("can't create texture ["..name.."] without file")
		end
		local texture = self.frame:CreateTexture()
		texture:SetTexture(cfg.file)
		texture:SetAllPoints(self.frame)
		if (cfg.texCoords) then
			texture:SetTexCoord(unpack(cfg.texCoords))
		end
		if (cfg.desaturated) then
			texture:SetDesaturated(cfg.desaturated)
		end
		if (cfg.rotation) then
			texture:SetRotation(unpack(cfg.rotation))
		end
		self.frame["Set"..name](self.frame, texture, cfg.alphaMode)
	end
end

local function _InitFrame(self)
	local frame = CreateFrame("Button", nil, UIParent)
	frame.view = self
	
	if (self.cfg.onClick) then
		frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
		frame:SetScript("OnClick", _OnClick)
	end
	
	self.frame = frame
	self.Icon = Icon
end

--[[ 
	Creates a new instance.
--]]
function yaka.Icon:new(cfgIn)
	local instance = setmetatable({}, self.metatable)
	instance:InitCfg(cfgIn)
	instance:InitFrame()
	return instance
end

function yaka.Icon.prototype:InitCfg(cfgIn)
	yaka.View.prototype.InitCfg(self, cfgIn)
	
	local cfg = self.cfg
	cfg.normalTexture = cfgIn.normalTexture
	cfg.pushedTexture = cfgIn.pushedTexture
	cfg.highlightTexture = cfgIn.highlightTexture
	cfg.disabledTexture = cfgIn.disabledTexture
end

function yaka.Icon.prototype:InitFrame()
	_InitFrame(self)
	
	yaka.View.prototype.InitFrame(self)
	
	local cfg = self.cfg
	_SetTexture(self, "NormalTexture", cfg.normalTexture)
	_SetTexture(self, "PushedTexture", cfg.pushedTexture)
	_SetTexture(self, "HighlightTexture", cfg.highlightTexture)
	_SetTexture(self, "DisabledTexture", cfg.disabledTexture)
end

function yaka.Icon.prototype:SetNormalTexture(texture)
	if ("string" == type(texture)) then
		self.frame:SetNormalTexture(texture)
	else
		self.cfg.normalTexture = texture
		_SetTexture(self, "NormalTexture", texture)
	end
end

function yaka.Icon.prototype:SetPushedTexture(texture)
	if ("string" == type(texture)) then
		self.frame:SetPushedTexture(texture)
	else
		self.cfg.pushedTexture = texture
		_SetTexture(self, "PushedTexture", texture)
	end
end

-- register view

YakaGui:RegisterViewType(yaka.Icon)